package vn.mobisoft.gachno;

import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity{
    /** Called when the activity is first created. */
	private EditText txtCode;
	private EditText txtPhone;
	private Button   btn_login;
	
	private Context content;
	//private Network network;
	
	private static final int 	DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressDialog 		mProgressDialog;
	private LoginAsync 			lgThread;
	private Boolean 			connectResult = false;
	private String 				httpResutl;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
/*        TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
        mPhoneNumber = tMgr.getLine1Number();*/

        content = this;
        
        txtCode = (EditText)findViewById(R.id.reg_editText_security);
        txtPhone = (EditText)findViewById(R.id.reg_editText_phonenumber);
        
        //TelephonyManager tMgr =(TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);
        //if (tMgr.getLine1Number().length()>0)  txtPhone.setText(tMgr.getLine1Number());
        //txtPhone.setText(tMgr.getSubscriberId());
        
        btn_login = (Button)findViewById(R.id.reg_button_go);
        btn_login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (txtCode.getText().toString().length()<=0 || txtPhone.getText().toString().length()<=0) {
					Toast.makeText(getApplicationContext(), getString(R.string.reg_thong_bao), Toast.LENGTH_LONG).show();
				}else {					
					//network.execute(CONSTANT.REG_LINK);		
					//((Global)getApplication()).updateCode(txtCode.getText().toString());
	    			lgThread = new LoginAsync();
	    			lgThread.execute(txtCode.getText().toString(), txtPhone.getText().toString());
				}				
			}
		});
        
        //network = new Network();
		//network.onConnectListener(this);
        //network = new Network();
        
    }
    
    
    
/*	@Override
	public void startNetworkConnect() {
		
	}
	
	@Override
	public void networkProgressUpdate(String... progress) {
		
	}
	
	@Override
	public void stopNetworkConnect(String result) {
		
		((Global)getApplication()).updateCode(txtCode.getText().toString());
		
		((Global)getApplication()).appendLog("Login at: " + Function.gethhmmss());
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
		
		if (result==null) {
			Toast.makeText(getApplicationContext(), getString(R.string.reg_login_fail), Toast.LENGTH_LONG).show();
			return;
		}else {		
			if (result.indexOf(R.string.reg_login_success)>=0) {
				Toast.makeText(getApplicationContext(), getString(R.string.reg_login_ok), Toast.LENGTH_LONG).show();
				((Global)getApplication()).appendLog("Login at: " + Function.gethhmmss());
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
			}else {
				Toast.makeText(getApplicationContext(), getString(R.string.reg_login_fail), Toast.LENGTH_LONG).show();
			}
		}	
	}
	
	@Override
	public void networkError(IOException e) {
		
	}*/
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DOWNLOAD_PROGRESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setTitle("");
			mProgressDialog.setMessage("Xin chờ trong giây lát!");
			mProgressDialog.setCancelable(true);
			mProgressDialog.show();
			return mProgressDialog;
		default:
			return null;
		}
	}
    
	/*
	 * login thread
	 */
	class LoginAsync extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			showDialog(DIALOG_DOWNLOAD_PROGRESS);
		}

		@Override
		protected String doInBackground(String... aurl) {
			connectResult = false;
			httpResutl = "";	
			
			String sLink = String.format(CONSTANT.LINK_LOGIN, aurl[0], aurl[1]);
			try {
				httpResutl = HttpConnect.getHttpStream(sLink);
				System.out.println("sLink:"+sLink);
				System.out.println("httpResutl:"+httpResutl);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if (httpResutl != null) {
				connectResult = true;
			}
			return null;
		}

		@Override
		protected void onCancelled() {

		}

		protected void onProgressUpdate(String... progress) {
			mProgressDialog.setProgress(Integer.parseInt(progress[0]));
		}

		@Override
		protected void onPostExecute(String unused) {
			if (!connectResult) {
				Toast.makeText(getApplicationContext(), "Lỗi kết nối!",
						Toast.LENGTH_SHORT);
			} else {
				checkResult();
			}
			dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
		}
	}
	

	public void checkResult() {
		System.out.println(httpResutl);
		if (httpResutl==null) {
			Toast.makeText(getApplicationContext(), getString(R.string.reg_login_fail), Toast.LENGTH_LONG).show();
			return;
		}else {		
			if (httpResutl.indexOf("0")==0 || httpResutl.length()<=0) {
				Toast.makeText(getApplicationContext(), getString(R.string.reg_login_fail), Toast.LENGTH_LONG).show();
			}else {
				Toast.makeText(getApplicationContext(), getString(R.string.reg_login_ok), Toast.LENGTH_LONG).show();
				((Global)getApplication()).appendLog("Đăng nhập: " + Function.gethhmmss());
				Intent intent = new Intent(this, GachNoActivity.class);
				startActivity(intent);
				((Global)getApplication()).updateCode(txtCode.getText().toString());
				((Global)getApplication()).updatePhone(txtPhone.getText().toString());
				((Global)getApplication()).updateMid(httpResutl.trim());
				finish();
			}
		}	
	}		
}